﻿using RedDragon.Common.Infrastructure.Interface;
using RedDragon.Common.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;

namespace RedDragon.Common.Infrastructure.Repositories
{
    public class BaseRepository<T> : IBaseRepository<T> where T : class,new()
    {


        private readonly ISqlSugarClient db;

        public BaseRepository(ISqlSugarClient db)
        {
            this.db = db;
        }

        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns>所有数据</returns>
        public async Task<ISugarQueryable<T>> GetAll()
        {
            return db.Queryable<T>();//异步方法
        }

        /// <summary>
        /// 没有异步获取所有数据
        /// </summary>
        /// <returns></returns>
        public List<T> Query()
        {
            return db.Queryable<T>().ToList();
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="eneity">实体</param>
        /// <returns></returns>
        public async Task<int> Add(T entity)
        {
            return await db.Insertable(entity).ExecuteCommandAsync();//异步方法
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="eneity">实体</param>
        /// <returns></returns>
        public async Task<int> Update(T entity)
        {
            return await db.Updateable(entity).ExecuteCommandAsync();
        }

        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="entities">实体</param>
        /// <returns></returns>
        public int AddRange(List<T> entities)
        {
            return db.Insertable(entities).ExecuteCommand();
        }

        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="entities">实体</param>
        /// <returns></returns>
        public int UpdRange(List<T> entities)
        {
            return db.Updateable(entities).ExecuteCommand();
        }
    }
}
